package com.wjs.juc.Future;

import org.junit.Test;

import java.util.UUID;
import java.util.concurrent.*;

public class CompletableFutureTest {
    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(3);
        try {
            CompletableFuture.supplyAsync(()->{
                String result= UUID.randomUUID().toString().substring(0,5);
                int i=10/0;
                return result;
            },pool).whenComplete((r,e)->{
                if(e==null){
                    System.out.println(r);
                }
            }).exceptionally((e)->{
                System.out.println(e.getCause()+"\t"+e.getMessage());
                return null;
            });
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            pool.shutdown();
        }
    }
    @Test
    public void test1() throws ExecutionException, InterruptedException {
        ExecutorService pool = Executors.newFixedThreadPool(3);
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace();}
            System.out.println("future运行了");
            System.out.println(Thread.currentThread().isDaemon());
            return "hh";
        },pool);
        System.out.println(Thread.currentThread().isDaemon());
        pool.shutdown();
    }
}
